package com.github.thinwonton.showcase.springboot.security.service;

import com.github.thinwonton.showcase.springboot.security.po.SystemUser;
import com.github.thinwonton.showcase.springboot.security.security.support.SystemUserDetails;
import com.github.thinwonton.showcase.springboot.security.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * UserDetailsService
 *
 * @author hugo
 * @date 2018/7/12
 */
@Service
public class JDBCUserDetailsService implements UserDetailsService {

    @Autowired
    private SystemUserService systemUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名获取用户信息
        SystemUser systemUser = systemUserService.getByUsername(username);
        if (systemUser == null) {
            throw new UsernameNotFoundException("没有找到用户[" + username + "]");
        }

        return new SystemUserDetails(systemUser);
    }
}
